import {login,listCountryaz} from '@/api/auth';
import {toast} from '@/utils/toast';
var msg = localStorage.getItem('msg')
const hand = localStorage.getItem('hand')
const BTCUSDT = JSON.parse(localStorage.getItem('BTCUSDT'))
const ETHUSDT = JSON.parse(localStorage.getItem('ETHUSDT'))
const LTCUSDT = JSON.parse(localStorage.getItem('LTCUSDT'))
const HS300USDT = JSON.parse(localStorage.getItem('HS300USDT'))
const IC500USDT =  JSON.parse(localStorage.getItem('IC500USDT'))
const product = localStorage.getItem('product')
const userinfo = localStorage.getItem('userinfo')
const currency = localStorage.getItem('currency')
const state = {
	userinfo: userinfo,
	listCountryaz: {},
	isLogin: false,
	guid: {},
	msg: '',
	guidinfo: '001',
	hand: hand,
	BTCUSDT: BTCUSDT,
	ETHUSDT: ETHUSDT,
	LTCUSDT: LTCUSDT,
	HS300USDT: HS300USDT,
	IC500USDT: IC500USDT,
	product:product,
	currency:currency


}
const mutations = {
	LOGIN_INFO: (state, info) => {
		state.userinfo = info;
		state.isLogin = true;
		uni.setStorage({
			key: 'userinfo',
			data: info
		})
	},
	Guid: (state) => {
		state.guid = guidinfo,
			uni.setStorage({
				key: 'guid',
				data: guidnfo
			})
	},
	HAND_NAME(state, info) {
		console.log(state, info)
		state.hand = info,
			uni.setStorage({
				key: 'hand',
				data: info
			})
	},
	logout(state) { //退出登录 
		console.log(state)
		state.userinfo = {}
		state.isLogin = false;
		uni.removeStorageSync('userinfo');
	},
	BTCUSDT(state, msg) {
		state.BTCUSDT = msg
		uni.setStorage({
			key: 'BTCUSDT',
			data: msg
		})

	},
	ETHUSDT(state, msg) {
		state.ETHUSDT = msg
		uni.setStorage({
			key: 'ETHUSDT',
			data: msg
		})
	},
	LTCUSDT(state, msg) {
		state.LTCUSDT = msg
		uni.setStorage({
			key: 'LTCUSDT',
			data: msg
		})
	},
	HS300USDT(state, msg) {
		state.HS300USDT = msg
		uni.setStorage({
			key: 'HS300USDT',
			data: msg
		})
	},
	IC500USDT(state, msg) {
		state.IC500USDT = msg
		uni.setStorage({
			key: 'IC500USDT',
			data: msg
		})

	},
	currency(state, msg) {
		state.currency = msg
		uni.setStorage({
			key: 'currency',
			data: msg
		})
	
	},
	
	listCountryaz: (state, listCountryaz) => {
		state.listCountryaz = listCountryaz;
	},
	changeLoginStatus(state, isLogin) {
		state.isLogin = isLogin;
	},
}
const actions = {
	changeLoginStatus: ({
		commit
	}, Bool) => {
		commit('changeLoginStatus', Bool)
	},
	loginInfo: ({
		commit
	}, form) => {
		login({ ...form
			})
			.then(res => {
				try {
					var info = JSON.parse(res.data);
				} catch (e) {
					var info = res.data;
				}
				if (info.code == 200) {
					toast('登录成功');
					uni.setStorage({
						key: 'flag',
						data: true
					})
					uni.switchTab({
						url: '/pages/quotation/main',
					})
					commit('LOGIN_INFO', info.data)
				} else {
					toast(info.msg)
				}
			})
	},
	listCountryaz: ({
		commit
	}, form) => {
		listCountryaz()
			.then(res => {
				if (res.data.code == 200) {
					commit('listCountryaz', res.data.data)
				}
			})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
